<?php
declare (strict_types = 1);

namespace app\model\communal;

use app\model\concern\AmisTrait;
use Carbon\Carbon;
use think\Model;
use think\db\Query;
use think\model\relation\BelongsTo;

class WebsiteNoticeModel extends Model
{
    use AmisTrait;

    /**
     * 数据库配置
     *
     * @var string
     */
    protected $connection = 'communal';

    /**
     * 主键
     *
     * @var string
     */
    protected $pk = 'id';

    /**
     * 表名
     *
     * @var string
     */
    protected $table = '91_website_notice';

    /**
     * 自动时间戳
     *
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 时间字段设置
     *
     * @var string
     */
    protected $createTime = 'created_at';
    protected $updateTime = 'updated_at';

    /**
     * 展示字段名
     *
     * @var string[]
     */
    public static $column = [
        'id', 'admin_id', 'content', 'relation_id', 'relation_type', 'type', 'notice_type', 'notice_category',
        'created_at'
    ];

    /**
     * 搜索字段名
     *
     * @var string[]
     */
    public static $searchColumn = [
        'id', 'admin_id', 'content', 'relation_id', 'relation_type', 'type', 'notice_category', 'createtime'
    ];

    /**
     * 新增字段
     *
     * @var string[]
     */
    public static $saveColumn = ['name', 'numbers', 'state'];

    /**
     * 更新字段
     *
     * @var string[]
     */
    public static $updateColumn = ['id', 'type'];

    public static $itemColumn = ['id', 'type'];

    /**
     * @var string[]
     */
    public static $logColumnMap = [
        'name' => '权益名称',
        'numbers' => '次数',
        'state' => '状态',
    ];


    /**
     * content 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchContentAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $query->where('content', 'like', '%' . $value . '%');
        }
    }

    /**
     * @param Query $query
     * @param $value
     */
    public function searchNoticeCategoryAttr(Query $query, $value)
    {
        if (!empty($value)) {
            $query->where('notice_category', $value);
        }
    }

    /**
     * type 搜索
     *
     * @param Query $query
     * @param $value
     */
    public function searchTypeAttr(Query $query, $value)
    {
        if (!is_null($value) && strlen((string)$value) > 0) {
            $currentStarAt = Carbon::now()->startOfDay()->timestamp;
            $currentEndAt = Carbon::now()->endOfDay()->timestamp;
            switch ($value) {
                case 3:
                    $query->where('type', $value);
                    break;
                case 102:
                    // 未处理
                    $query->where('type', 1);
                    $query->where('created_at', '>=', $currentStarAt);
                    $query->where('created_at', '<=', $currentEndAt);
                    break;
                case 103:
                    // 已逾期
                    $query->where('type', 1);
                    $query->where('created_at', '>=', $currentEndAt);
                    break;
            }

        }
    }
}
